<?php

namespace App\Http\Controllers\Admin;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class StateController extends BaseController
{
    public function change(Request $request)
    {
        $validator = Validator::make($request->all(),[
            'changeType' => 'required|in:article,product,productSku,focus',
            'changeKey' => 'required|in:weight,online,is_hot,is_recom,sku,origin_price,price,price_3_4,price_5,consume_price',
            'changeValue' => 'required',
            'changeId' => 'required',
        ]);

        if ($validator->fails()) {
            return $this->failed($this->error = $validator->errors()->first());
        }

        $type  = strtolower($request->input('changeType'));
        $key   = $request->input('changeKey');
        $value = $request->input('changeValue');
        $id    = $request->input('changeId',0);

        if($key == "online" && !in_array($value,['yes','no'])) {
            return $this->failed('online值参数错误');
        }

        $modelArray = [
            'article' => 'App\Model\Article',
            'productsku' => 'App\Model\ProductSku',
            'product' => 'App\Model\WineProduct',
            'focus'=>'App\Model\WineFocusPicture'
        ];

        $model = new $modelArray[$type];

        if($model = $model->find($id)) {
            $model->$key = $value;
            if($model->beforeSave() && $model->save() && $model->afterSave()){
                return $this->success($model);
            }
            return $this->failed('修改失败');
        }else{
            return $this->notFond();
        }


    }

    public function dealer(Request $request)
    {
        $validator = Validator::make($request->all(),[
            'userIds' => 'required',
            'state' => 'required|in:active,stop',
        ]);

        if ($validator->fails()) {
            return $this->failed($this->error = $validator->errors()->first());
        }

        $userIds = explode(',',$request->input('userIds'));
        $state = $request->input('state');
        $errors = [];
        $i = 0;
        if ($state == "active"){
            foreach ($userIds as $id)
            {
                $user = User::find($id);
                if (!$user){
                    $errors[] = '#'.$id.' 用户不存在！';
                    continue;
                }

                if ($user->is_dealer == 1){
                    $errors[] = '#'.$id.' 用户已是经销商！';
                    continue;
                }

                if ($user->parent_id!=0){
                    $errors[] = '#'.$id.' 用户是ID:'.$user->id.'的下线，不能置为经销商！';
                    continue;
                }

                $user->is_dealer =1;
                $user->active_dealer =1;
                $user->save();
                $i++;
                DB::table('t_user')->where('parent_dealer_id','=',$user->id)->update(['active_dealer'=>1]);

            }

            return $this->success(['total'=>count($userIds),'success'=>$i,'errors'=>$errors]);
        }else{
            foreach ($userIds as $id)
            {
                $user = User::find($id);
                if (!$user){
                    $errors[] = '#'.$id.' 用户不存在！';
                    continue;
                }

                if ($user->is_dealer != 1){
                    $errors[] = '#'.$id.' 用户不是经销商！';
                    continue;
                }

                $user->is_dealer = 0;
                $user->active_dealer = 0;
                $user->save();
                $i++;
                DB::table('t_user')->where('parent_dealer_id','=',$user->id)->update(['active_dealer'=>0]);
            }

            return $this->success(['total'=>count($userIds),'success'=>$i,'errors'=>$errors]);
        }

    }
}
